<?php
// This file goes through the pickupPath directoy and removes all folder and files based on a time limit.
include_once("../config.php");

# days
$limit = (trim($_REQUEST['limit']) != '') ? trim($_REQUEST['limit']) : 3;
$timeLimit = mktime(0,0,0,date('m'),date('d') - $limit,date('Y'));
$dir = $pickUpPath;
$dirCount = 0;
$fileCount = 0;

if($handle = opendir($dir)){
	while($file = readdir($handle)) {
		if($file == '.' || $file == '..') continue;
		$time = @filemtime($dir.$file);

print $time  . "::" . $timeLimit;

		if($time < $timeLimit){
			if (is_dir($dir.$file)) {
				rrmdir($dir.$file);
				$dirCount++;
			} else if(is_file($dir.$file)) {
				@unlink($dir.$file);
				$fileCount++;
			}
		}
	}
  closedir($handle);
}

function rrmdir($dir) {
 if (is_dir($dir)) {
	 $objects = scandir($dir);
	 foreach ($objects as $object) {
		 if ($object != '.' && $object != '..') {
			if (filetype($dir.'/'.$object) == 'dir') {
				rrmdir($dir.'/'.$object); 
			} else {
				unlink($dir.'/'.$object);
			}
		 }
	 }
	 reset($objects);
	 rmdir($dir);
 }
}

echo '<br>Script executed successfully.';
echo '<br>Directories Deleted: ' . $dirCount;
echo '<br>Files Deleted: ' . $fileCount;

?>